#include <pic16f913.h>


void fnInitSSP(void)
{
	STATUSbits.RP1 = 0;
	STATUSbits.RP0 = 0;
	SSPCON = 0X32;
	STATUSbits.RP0 = 1;
	SSPSTAT = 0x00;			//	SMP = 0,CKE = 0
}

unsigned char fnGetSensorVal(void)
{
	unsigned char val,tmp;

	GIE = 0;
	PORTCbits.RC5 = 0;
	SSPBUF = 0xc0 + 0x2b;		//0X00 or 0x2c
	while(!BF);
	val = SSPBUF;

	SSPBUF = 0xa5;			//random
	while(!BF);
	val = SSPBUF;

	SSPBUF = 0x81;			//random
	while(!BF);
	val = SSPBUF;
	val = ~val;

	PORTCbits.RC5 = 1;
	GIE = 1;
	return val;
}

unsigned char fnGetSensorReg(unsigned char reg)
{
	unsigned char val;

	GIE = 0;
	PORTCbits.RC5 = 0;
	SSPBUF = 0x80 + reg;		
	while(!BF);
	val = SSPBUF;

	SSPBUF = 0x00;			//random
	while(!BF);
	val = SSPBUF;
#ifdef SINGLE_CHIP
	val = ~val;
#endif
	PORTCbits.RC5 = 1;
	GIE = 1;
	return val;
}

void fnWriteSensorReg(unsigned char reg,unsigned char val)
{
	GIE = 0;
	PORTCbits.RC5 = 0;

	SSPBUF = reg & 0x7f;		
	while(!BF);
	SSPBUF = val;			//random
	while(!BF);
	PORTCbits.RC5 = 1;
	GIE = 1;
}
